package com.llkj.safeearth;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.llkj.safeearth.httpUtils.HttpMethod;
import com.llkj.safeearth.utils.SpUtil;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.yayandroid.locationmanager.LocationManager;
import com.yayandroid.locationmanager.configuration.DefaultProviderConfiguration;
import com.yayandroid.locationmanager.configuration.GooglePlayServicesConfiguration;
import com.yayandroid.locationmanager.configuration.LocationConfiguration;
import com.yayandroid.locationmanager.configuration.PermissionConfiguration;
import com.yayandroid.locationmanager.listener.LocationListener;

import butterknife.ButterKnife;
import cn.jpush.android.api.InstrumentedActivity;
import io.reactivex.FlowableEmitter;
import io.reactivex.internal.disposables.ListCompositeDisposable;
import rx.functions.Action1;


/**
 *
 */
public abstract class BaseActivity extends InstrumentedActivity{
	private ListCompositeDisposable listCompositeDisposable;
	protected String TAG = this.getClass().getSimpleName();
	protected Context mContext;
	protected ProgressDialog pd;
	private View emptyView;
	private View contentView;
	public static final int pageSize = 20;
	private FlowableEmitter flowableEmitter;

	public static final String DATA = "data";
	public static final String ID = "id";
	public static final String LIST = "list";
	private volatile static int act_in_foreground = 0;
	protected boolean mPermission = false;
	public SpUtil sputil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		beforeSetContentView(savedInstanceState);

		setContentView(getLayoutId());
		new RxPermissions(this)
				.request(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE)
				.subscribe(new Action1<Boolean>() {
					@Override
					public void call(Boolean aBoolean) {
						mPermission = aBoolean;
					}
				});

		sputil = SpUtil.init(getApplicationContext());

		ButterKnife.bind(this);
		mContext = this;
		afterSetContentView(savedInstanceState);

		startService(new Intent(this,LocationService.class));

		initView();
		initData();
		initListener();

	}


	public static boolean isForeground() {
		return act_in_foreground > 0;
	}

	protected void initView() {
		emptyView = getEmptyView();
	}

	protected void initData() {
	}

	protected void initListener() {
	}

	protected void beforeSetContentView(Bundle savedInstanceState) {
	}

	protected void afterSetContentView(Bundle savedInstanceState) {
		if (Build.VERSION.SDK_INT >= 21) {
			View decorView = getWindow().getDecorView();
			int option = /*View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
					|*/ View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
			decorView.setSystemUiVisibility(option);
			getWindow().setStatusBarColor(Color.parseColor("#8076C6"));
		}
	}

	protected abstract int getLayoutId();


	@Override
	protected void onPause() {
		super.onPause();
	}

	@CallSuper
	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	public void onStart() {
		super.onStart();
		act_in_foreground++;
	}

	@Override
	public void onStop() {
		super.onStop();
		act_in_foreground--;
	}
	@CallSuper
	@Override
	protected void onDestroy() {
		clear();
		mContext = null;

		super.onDestroy();
	}

	public ListCompositeDisposable getComp() {
		if (this.listCompositeDisposable == null) {
			this.listCompositeDisposable = new ListCompositeDisposable();
		}
		return listCompositeDisposable;
	}

	protected void clear() {
		if (!getComp().isDisposed()) {
			getComp().clear();
		}
	}

	@Override
	public Resources getResources() {
		Resources res = super.getResources();
		Configuration config = new Configuration();
		config.setToDefaults();
		res.updateConfiguration(config, res.getDisplayMetrics());
		return res;
	}

	/**
	 * [页面跳转]
	 *
	 * @param clz
	 */
	public void startActivity(Class<?> clz, @Nullable Intent data) {
		Intent intent;
		if (data != null) {
			intent = new Intent(data);
		} else {
			intent = new Intent();
		}
		intent.setClass(mContext, clz);
		startActivity(intent);
	}

	/**
	 * 全局等待对话框
	 */
	public void showWaitDialog() {
		//如果ctx等于空或者isFinishing
		if (mContext == null || isFinishing())
			return;
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (pd == null
						|| !pd.isShowing()) {
					pd = new ProgressDialog(mContext);
					pd.setMessage("加载中...");
					pd.setCancelable(true);
					pd.show();
				}

			}
		});

	}

	public void dismissWaitDialog() {
		if (mContext == null || isFinishing())
			return;
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (pd != null
						&& pd.isShowing()) {
					pd.dismiss();
					pd = null;
				}
			}
		});

	}

	/**
	 * 暂时先别调用这个方法,还没有处理好.
	 */
	@Deprecated
	protected View getEmptyView() {
		return null;
	}

	/**
	 * 暂时先别调用这个方法,还没有处理好.
	 */
	@Deprecated
	public void showEmptyView(boolean show) {
		if (show && emptyView != null) {
			ViewGroup contentParent = (ViewGroup) findViewById(android.R.id.content);
			contentView = contentParent.getChildAt(0);
			contentParent.removeAllViews();
			contentParent.addView(emptyView);
		} else {
			ViewGroup contentParent = (ViewGroup) findViewById(android.R.id.content);
			contentParent.removeAllViews();
			contentParent.addView(contentView);
		}
	}

}
